package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//预约列表
@RestController
@RequestMapping(path = "/order")
@CrossOrigin
public class OrderController {

    @Reference(version = "1.0.0")
    private OrderService orderService;

    /**
     * 保存预约信息
     *
     * @param order
     * @return
     */
    @PostMapping(path = "/add")
    public Result add(@RequestBody Order order) {
        try {

          String result = orderService.add(order);
          if("预约满了".equals(result)){
              return new Result(false, MessageConstant.ORDER_FULL);
          }
            return new Result(true, MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    /**
     * 分页查询预约信息
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result findPage(OrderQueryPageBean orderQueryPageBean) {
        //分页查询预约信息
        try {

            PageResult pageResult = orderService.pageQuery(orderQueryPageBean);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据id查询个人预约详情
     *
     * @param id
     * @return
     */
    @GetMapping( "/{id}")
    public Result findById(@PathVariable("id") String id) {
        try {
            Order order = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }



    /**
     * 更新预约信息
     *
     * @param order
     * @return
     */
    @PostMapping(path = "/submit")
    public Result update(@RequestBody Order order) {

        try {

            String result =  orderService.update(order);
            if("预约满了".equals(result)){
                return new Result(false, MessageConstant.ORDER_FULL);
            }
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /**
     * 查询所有预约信息
     *
     * @return
     */
    @GetMapping(path = "/findAll")
    public Result findAll() {
        try {
            List<Order> orderList = orderService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }



    @PostMapping("updateArrival")
    public Result updateArrival(@RequestBody Order order){
        try {
            orderService.updateArrival(order);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ORDERSETTING_FAIL);
        }

    }
}
